#include <windows.h>
#include "CChar.h"
#include "CQuestMissionLogPacket.h"

CQuestMissionLogPacket::CQuestMissionLogPacket(CChar * schar, unsigned char logID, unsigned char status) {
	this->type = 0x56;
	this->size = 0x14; 
	memset(data, 0, sizeof(data));

	unsigned short logType = 0x0000;
	switch(logID) {
		case AREA_SANDORIA:
			generateQuestPacket(schar, AREA_SANDORIA, status);
			if(status == 0x01)
				logType = SAN_CURRENT;
			if(status == 0x02)
				logType = SAN_COMPLETE;
			break;
		case AREA_BASTOK:
			generateQuestPacket(schar, AREA_BASTOK, status);
			if(status == 0x01)
				logType = BAS_CURRENT;
			if(status == 0x02)
				logType = BAS_COMPLETE;
			break;
		case AREA_WINDURST:
			generateQuestPacket(schar, AREA_WINDURST, status);
			if(status == 0x01)
				logType = WIN_CURRENT;
			if(status == 0x02)
				logType = WIN_COMPLETE;
			break;
		case AREA_JEUNO:
			generateQuestPacket(schar, AREA_JEUNO, status);
			if(status == 0x01)
				logType = JEU_CURRENT;
			if(status == 0x02)
				logType = JEU_COMPLETE;
			break;
		case AREA_OTHER:
			generateQuestPacket(schar, AREA_OTHER, status);
			if(status == 0x01)
				logType = OTHER_CURRENT;
			if(status == 0x02)
				logType = OTHER_COMPLETE;
			break;
		case AREA_OUTLANDS:
			generateQuestPacket(schar, AREA_OUTLANDS, status);
			if(status == 0x01)
				logType = OUT_CURRENT;
			if(status == 0x02)
				logType = OUT_COMPLETE;
			break;
		case AREA_CRYSTALWAR:
			generateQuestPacket(schar, AREA_CRYSTALWAR, status);
			if(status == 0x01)
				logType = WAR_CURRENT;
			if(status == 0x02)
				logType = WAR_COMPLETE;
			break;

		case AREA_AHTURHGAN:
		case MISSION_ASSAULT:
			if(status == 0x02) {
				generateQuestPacket(schar, AREA_AHTURHGAN, status);
				logType = EXP_COMPLETE;
				for(unsigned char questMissionID = 0; questMissionID <= 127; questMissionID++)
					data[(questMissionID/8)+0x10] ^= ((schar->m_assaultLog.complete[questMissionID]) << (questMissionID % 8));
				break;
			}
		case MISSION_TOAU:
		case MISSION_WOTG:
			if(status == 0x02) {
				logType = EXP_MISS_COMPLETE;
				for(unsigned char logID = 0x04; logID <= 0x05; logID++)
					for(unsigned char questMissionID = 0; questMissionID <= 63; questMissionID++)
						data[(questMissionID/8) + ((logID-0x04)*0x08)] ^= ((schar->m_missionLogs[logID].complete[questMissionID]) << (questMissionID % 8));
				break;
			}
		case MISSION_CAMPAIGN:
			if(status == 0x02) {
				logType = CAMPAIGN1;
				for(unsigned short questMissionID = 0; questMissionID <= 255; questMissionID++)
					data[(questMissionID/8)] ^= ((schar->m_campaignLog.complete[questMissionID]) << (questMissionID % 8));
				break;
			}
		case MISSION_CAMPAIGN2:
			if(status == 0x02) {
				logType = CAMPAIGN2;
				for(unsigned short questMissionID = 0; questMissionID <= 255; questMissionID++)
					data[(questMissionID/8)] ^= ((schar->m_campaignLog.complete[questMissionID+256]) << (questMissionID % 8));
				break;
			}
			if(status == 0x01) {
				logType = EXP_CURRENT;
				generateQuestPacket(schar, AREA_AHTURHGAN, status);
				memcpy(data+0x18-0x04, &schar->m_missionLogs[MISSION_TOAU-10].current, 4);
				memcpy(data+0x0C-0x04, &schar->m_missionLogs[MISSION_WOTG-10].current, 4);
				memcpy(data+0x1C-0x04, &schar->m_assaultLog.current, 4);
				memcpy(data+0x20-0x04, &schar->m_campaignLog.current, 4);
				break;
			}
		case MISSION_SANDORIA:
		case MISSION_BASTOK:
		case MISSION_WINDURST:
		case MISSION_ZILART:
			if(status == 0x02) {
				logType = MISS_COMPLETE;
				for(unsigned char logID = 0x00; logID <= 0x03; logID++)
					for(unsigned char questMissionID = 0; questMissionID < 64; questMissionID++)
						data[(questMissionID/8) + (logID*0x08)] ^= ((schar->m_missionLogs[logID].complete[questMissionID]) << (questMissionID % 8));
				break;
			}
		case MISSION_COP:
		case MISSION_ACP:
			if(status == 0x01) {
				logType = MISS_CURRENT;
				memcpy(data+0x04-0x04, &schar->st.n.home, 1);
				memcpy(data+0x08-0x04, &schar->m_missionLogs[schar->st.n.home].current, 4);
				memcpy(data+0x0C-0x04, &schar->m_missionLogs[MISSION_ZILART-10].current, 4);
				unsigned long chains = 0;
				chains = schar->m_copCurrent;
				chains = ((chains * 0x08) + 0x60);
				memcpy(data+0x10-0x04, &chains, 4);
				memcpy(data+0x18-0x04, &schar->m_acpCurrent, 4);
				break;
			}
	}

	memcpy(data+0x24-0x04, &logType, 2);
}

void CQuestMissionLogPacket::generateQuestPacket(CChar * schar, unsigned char logID, unsigned char status) {
	if(status == 0x01)
		for(unsigned short i = 0; i < 256; i++)
			data[i/8] ^= ((schar->m_questLogs[logID].current[i]) << (i % 8));//schar->m_questLogs[logID].current[i]
	else if(status == 0x02)
		for(unsigned short i = 0; i < 256; i++)
			data[i/8] ^= ((schar->m_questLogs[logID].complete[i]) << (i % 8));//schar->m_questLogs[logID].complete[i]
}

CQuestMissionLogPacket::~CQuestMissionLogPacket() {

}
